package com.liaojp.sccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liaojp.sccp.common.Result;
import com.liaojp.sccp.entity.Users;
import com.liaojp.sccp.service.UsersService;
import com.liaojp.sccp.mapper.UsersMapper;
import com.liaojp.sccp.util.JwtUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @author 廖家鹏
* @description 针对表【users(用户表)】的数据库操作Service实现
* @createDate 2025-04-29 00:36:02
*/
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
    implements UsersService{
    private final PasswordEncoder passwordEncoder;
    private final JwtUtils jwtUtils;

    public UsersServiceImpl(PasswordEncoder passwordEncoder, JwtUtils jwtUtils) {
        this.passwordEncoder = passwordEncoder;
        this.jwtUtils = jwtUtils;
    }


    @Override
    public Result login(String username, String password) {
//        String bcryptPassword = passwordEncoder.encode("123456");
//        Users users = new Users();
//        users.setPassword(bcryptPassword);
//        users.setUsername("admin");
//        users.setRole("admin");
//        this.save(users);
        // 验证登录信息
        if (!isFormat(username, password)){
            return new Result().fail("用户名或密码不能为空");
        }

        // 查询user表
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Users> eq = wrapper
                .eq(Users::getUsername, username);
        Users user = this.getOne(eq);

        if (user == null){
            return new Result().fail("用户名不存在");
        }

        if (!passwordEncoder.matches(password, user.getPassword())){
            return new Result().fail("用户名或密码错误");
        }
        // 返回对应信息
        String token = jwtUtils.generateToken(new HashMap<>(), user.getUserId());
        Map<String, Object> map = Map.of("userId", user.getUserId(), "username", user.getUsername(), "role", user.getRole(), "token", token);

        return Result.success(map);
    }

    @Override
    public Result register(String username, String password) {
        if (!isFormat(username, password)){
            return new Result().fail("用户名或密码不能为空");
        }

        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<Users>()
                .eq(Users::getUsername, username);
        if (this.getOne(queryWrapper) != null){
            return new Result().fail("用户名已存在");
        }

        Users users = new Users();
        users.setUsername(username);
        users.setPassword(passwordEncoder.encode(password));
        users.setRole("guest");
        users.setCreateBy("-1"); // -1表示自己注册
        users.setCreateTime(new Date());

        this.save(users);

        return Result.success(new HashMap<>());
    }

    private boolean isFormat(String username, String password){
        return username != null && password != null && !username.isEmpty() && !password.isEmpty();
    }
}




